{% extends 'configmanager/__base__.html' %}
{% block title %}发布站点列表 | {% endblock %}
{% block search_display %}none{% endblock %}
{% block content %}


    <div id="whole" class="container-fluid table-responsive">
        <div>
            <a href="{% url 'configmanager:applychange' apply.id %}" type="button" class="btn btn-default">返回</a>
        </div>
        {% if apply %}
            <table class="table table-striped table-hover table-condensed table-bordered">
                <caption><strong>发布编号：{{ apply.applyproject }}</strong></caption>
                <thead>
                <tr>
                    <th>发布优先级</th>
                    <th>代码分支</th>
                    <th>Jenkins版本号</th>
                    <th>站点名称</th>
                    <th>发布注意事项</th>
                    <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;所属ECS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;配置状态&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作</th>
                    <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;所属ECS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SLB状态&nbsp;&nbsp;&nbsp;健康状态&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作</th>
                    <th>健康检查</th>
                    <th>&nbsp;&nbsp;&nbsp;&nbsp;所属ECS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;发布状态&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作</th>
                </tr>
                </thead>
                <tbody>
                {% for site in apply.deployitem_set.all %}
                    <tr>
                        <td>{{ site.deployorderby }}</td>
                        <td>{% if site.type == "T" %}主干{% else %}分支{% endif %}</td>
                        <td>{{ site.jenkinsversion }}</td>
                        <td>
                            <a href="{% url 'configmanager:sitechange' site.deploysite.id %}"
                               target="_blank"><span>{{ site.deploysite.fullname }}</span></a>
                        </td>
                        <td>{{ site.deploysite.deployattention }}</td>
                        <td align="right">
                            {% for r in site.deploysite.release_set.all %}
                                <p>
                                    {{ r.ECS }}&nbsp;&nbsp;&nbsp;
                                    {% if r.status == "N" %}
                                        <span class="text-danger"><strong>待生效</strong></span>&nbsp;&nbsp;&nbsp;
                                    {% endif %}
                                    {% if r.status == "Y" %}
                                        已生效&nbsp;&nbsp;&nbsp;
                                    {% endif %}
                                    <a class="btn btn-primary" href="">浏览测试页</a>
                                </p>
                            {% endfor %}
                        </td>
                        <td id="td_{{ site.id }}" align="right">
                            {% for ecs in site.deploysite.get_one_slb.slbhealthstatus_set.all %}
                                <p>
                                    <span>{{ ecs.ECS }}</span>&nbsp;&nbsp;
                                    <span>{% if ecs.SLBstatus == "added" %}已添加{% else %}<span
                                            class="text-danger"><strong>已移除</strong></span>{% endif %}</span>&nbsp;&nbsp;
                                    <span>
                                {% if ecs.SLBstatus == "added" %}
                                    {% if ecs.healthstatus == "normal" %}&nbsp;&nbsp;&nbsp;&nbsp;正常&nbsp;&nbsp;&nbsp;
                                        &nbsp;
                                    {% elif ecs.healthstatus == "abnormal" %}<span
                                            class="text-danger"><strong>异常</strong></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                    {% else %}<span
                                            class="text-danger"><strong>正在检查</strong></span>&nbsp;
                                    {% endif %}
                                {% else %}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                {% endif %}
                            </span>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <span>
                                {% if ecs.SLBstatus == "added" %}<a class="btn btn-warning" href="javascript:void(0)"
                                                                    onclick="SiteRemoveBackendServer('{{ site.deploysite.id }}', '{{ site.deploysite.fullname }}', '{{ site.id }}', '{{ ecs.ECS.id }}', '{{ ecs.ECS.name }}')">移除</a>{% endif %}
                                        {% if ecs.SLBstatus == "removed" %}
                                            <a class="btn btn-success" href="javascript:void(0)"
                                               onclick="SiteAddBackendServer('{{ site.deploysite.id }}', '{{ site.deploysite.fullname }}', '{{ site.id }}', '{{ ecs.ECS.id }}', '{{ ecs.ECS.name }}')">添加</a>{% endif %}
                            </span>
                                <p/>
                            {% endfor %}
                        </td>
                        <td><a class="btn btn-info" href="javascript:void(0)"
                               onclick="MoreHealthRefresh('{{ site.deploysite.id }}', '{{ site.deploysite.fullname }}', '{{ site.id }}')">刷新</a>
                        </td>
                        <td align="right">
                            {% for d in site.deploysite.deployecs_set.all %}
                                {% if d.applyproject_id == apply.id %}
                                    <p>
                                        <span>{{ d.ECS }}</span>&nbsp;&nbsp;
                                        <span>{% if d.ECSdeploystatus == "N" %}
                                            <span class="text-danger"><strong>待发布</strong></span>{% else %}<span
                                                    class="text-success"><strong>已发布</strong></span>{% endif %}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                        <span>
                                {% if d.ECSdeploystatus == "N" %}
                                    {% for r in d.site.release_set.all %}
                                        {% if r.ECS.id == d.ECS.id %}
                                            {% if r.status == "N" %}
                                                <a class="btn btn-primary"
                                                   href="{% url 'configmanager:applyconfigdeploy' deployecs_id=d.id release_id=r.id %}">发布</a>
                                                &nbsp;&nbsp;&nbsp;&nbsp;
                                            {% else %}
                                                <a class="btn btn-success"
                                                   href="{% url 'configmanager:applyconfigdeploy' deployecs_id=d.id release_id=r.id %}">标记为已发布</a>
                                            {% endif %}
                                        {% endif %}
                                    {% endfor %}
                                {% endif %}
                            </span>
                                    </p>
                                {% endif %}
                            {% endfor %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% else %}
            <p>No deploysite are available.</p>
        {% endif %}
    </div>
    <br>


    <script>
        $.ajaxSetup({
            data: {csrfmiddlewaretoken: '{{ csrf_token }}'},
        });

        //局部刷新
        function PartialRefresh(siteid) {
            var postUrl = "/apply/" + siteid + "/partrefresh/"
            $.post(postUrl, function (data) {
                $('#td_' + siteid).html(data)
            });
        }

        //刷新SLB健康状态
        function MoreHealthRefresh(siteid, sitename, deploysiteid) {
            var postUrl = "/slb/" + siteid + "/moreslbhealthupdate/";

            jQuery('#body').showLoading();

            $.post(postUrl, function (data) {
                if (data.success) {
                    PartialRefresh(deploysiteid);
                    $("#alert-div").css("display", "");
                    $("#alert-div").attr("class", "alert alert-success");
                    $("#alert-div").text("成功！刷新 " + sitename + " 关联SLB健康状态");
                    setTimeout(function () {
                        $("#alert-div").fadeOut();
                    }, 20000);
                } else {
                    alert("操作失败！" + data.message);
                }
                setTimeout(function () {
                    jQuery('#body').hideLoading();
                }, 800);
            });
        }


        //移除站点所属SLB后端服务器
        function SiteRemoveBackendServer(siteid, sitename, deploysiteid, serverid, servername) {
            var postUrl = "/slb/" + siteid + "/siteremovebackendserver/" + serverid + "/";

            jQuery('#body').showLoading();

            $.post(postUrl, function (data) {
                if (data.success) {
                    PartialRefresh(deploysiteid);
                    $("#alert-div").css("display", "");
                    $("#alert-div").attr("class", "alert alert-success");
                    $("#alert-div").text("成功！移除站点 " + sitename + " 关联SLB的后端服务器 " + servername);
                    setTimeout(function () {
                        $("#alert-div").fadeOut();
                    }, 20000);
                } else {
                    alert("操作失败！" + data.message);
                }
                setTimeout(function () {
                    jQuery('#body').hideLoading();
                }, 300);
            });
        }


        //添加站点所属SLB后端服务器
        function SiteAddBackendServer(siteid, sitename, deploysiteid, serverid, servername) {
            var postUrl = "/slb/" + siteid + "/siteaddbackendserver/" + serverid + "/";

            jQuery('#body').showLoading();

            $.post(postUrl, function (data) {
                if (data.success) {
                    PartialRefresh(deploysiteid);
                    $("#alert-div").css("display", "");
                    $("#alert-div").attr("class", "alert alert-success");
                    $("#alert-div").text("成功！新增站点 " + sitename + " 关联SLB的后端服务器 " + servername);
                    setTimeout(function () {
                        $("#alert-div").fadeOut();
                    }, 20000);
                } else {
                    alert("操作失败！" + data.message);
                }
                setTimeout(function () {
                    jQuery('#body').hideLoading();
                }, 300);
            });
        }


    </script>

{% endblock %}
